package com.foreverwin.jzymes.report.picking.service.impl;



import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.production.picking.domain.PickingBillLog;
import com.foreverwin.jzymes.production.picking.domain.PickingBom;
import com.foreverwin.jzymes.production.picking.enums.PickingstatusEnum;
import com.foreverwin.jzymes.production.picking.service.IPickingBillLogService;
import com.foreverwin.jzymes.report.picking.mapper.PickingReportMapper;
import com.foreverwin.jzymes.report.picking.service.IPickingReportService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PickingReportServiceImpl implements IPickingReportService {
    @Autowired
    PickingReportMapper pickingReportMapper;
    @Autowired
    IPickingBillLogService pickingBillLogService;

    /**
     * 领料单
     * @param jsonMap
     * @return
     */
    public List<PickingBillLog> selectPickingReport( Map<String,Object> jsonMap ){
        String site = (String)jsonMap.get("site");
        String bill = (String)jsonMap.get("bill");

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if ( !StringUtils.isBlank( (String) jsonMap.get( "startDate" ) ) ){
            try {
                startDate = sdf.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( !StringUtils.isBlank( (String) jsonMap.get( "endDate" ) ) ){
            try {
                endDate = sdf.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List< PickingBillLog > pickingBillLogs = new ArrayList<>();

        if (StringUtils.isBlank( bill )){
            pickingBillLogs = pickingReportMapper.selectPickingByReport( site , startDate ,endDate );

            for ( PickingBillLog picking : pickingBillLogs ){
                changeStatus( picking );
            }

        }else {
           String handle = BOHandleUtil.buildPickingBillLogHandle( site , bill );
           PickingBillLog pickingBillLog  = pickingBillLogService.selectPickingBillLogById( handle );
           changeStatus( pickingBillLog );
           pickingBillLogs.add( pickingBillLog );
        }


        return pickingBillLogs;
    }

    /**
     * 领料状态更改中文
     * @param pickingBillLog
     */
    public void changeStatus ( PickingBillLog pickingBillLog ){
       String status = pickingBillLog.getStatus();
       if ( status.equals( PickingstatusEnum.NEW.name() ) ){
           status = "新建";
       }
        if ( status.equals( PickingstatusEnum.SIGN.name() ) ){
            status = "OA送签";
        }
        if ( status.equals( PickingstatusEnum.FAIL.name() ) ){
            status = "ERP审核不通过";
        }
        if ( status.equals( PickingstatusEnum.PASS.name() ) ){
            status = "ERP审核通过";
        }
        pickingBillLog.setStatus( status );
    }

    /**
     * 领料明细查询
     * @param jsonMap
     * @return
     */
    @Override
    public List<PickingBom> findPickingBom(Map<String, Object> jsonMap) {
        String handle = (String)jsonMap.get("handle");
        String bill = (String)jsonMap.get("bill");
      /*  Date createdDateTime = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ( !StringUtils.isBlank( (String)jsonMap.get("createdDateTime") ) ){
            try {
                createdDateTime = simpleDateFormat.parse((String)jsonMap.get("createdDateTime"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }*/

        List< PickingBom > pickingBoms = pickingReportMapper.findPickingBom( handle );
        for ( PickingBom pickingBom : pickingBoms ){
            pickingBom.setStatus( bill );
        }

        return pickingBoms;
    }
}
